package com.ss.android.article.base.feature.feed.activity;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.base.feature.feed.c.FilterWord;
import com.ss.android.article.base.feature.feed.c.b;
import com.ss.android.article.base.feature.feed.dd;
import com.ss.android.article.base.feature.feed.ff;
import com.ss.android.article.base.feature.model.CellRef;
import com.ss.android.article.base.ui.BaseDialog;
import com.ss.android.article.browser.R;
import com.ss.android.common.callback.SSCallback;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.WeakHashMap;

public final class ai extends BaseDialog implements ff {
    public static WeakHashMap k = new WeakHashMap();
    public ImageView mTopArrow;
    public ImageView mBottomArrow;
    Resources a;
    LayoutInflater mLayoutInflater;
    SSCallback mSSCallback;
    CellRef mCellRef;
    dd.WindowFocusChangeListener g;
    int h;
    View i;
    TextView j;
    View.OnClickListener l;
    private DislikeRelativeLayout mDislikeRelativeLayout;
    private RelativeLayout mMainLayout;
    private Context p;
    private View q;
    private ListView r;
    private a s;
    private List t;
    private boolean u;

    public ai(Activity activity, CellRef cellRef, SSCallback ssCallback) {
        super(activity, R.style.jz);
        ViewGroup v4 = null;
        this.t = new ArrayList();
        this.h = 0;
        new ArrayList();
        this.l = new View.OnClickListener() {

            public final void onClick(View arg5) {
                Object v0 = arg5.getTag();
                if (mSSCallback != null && ((v0 instanceof com.ss.android.article.base.feature.feed.c.b))) {
                    if (mCellRef != null) {
                        mCellRef.r();
                    }

                    if (mSSCallback != null && ((v0 instanceof b))) {
                        mSSCallback.onCallback(new Object[]{v0});
                    }

                    f();
                }
            }
        };
        this.p = ((Context) activity);
        this.a = activity.getResources();
        this.mLayoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mDislikeRelativeLayout = (DislikeRelativeLayout) this.mLayoutInflater.inflate(R.layout.dislike_view, v4);
        this.mSSCallback = ssCallback;
        this.mCellRef = cellRef;
        if (this.mDislikeRelativeLayout != null) {
            this.mMainLayout = this.mDislikeRelativeLayout.findViewById(com.ss.android.article.browser.R.id.main_layout);
            this.mTopArrow = this.mDislikeRelativeLayout.findViewById(com.ss.android.article.browser.R.id.top_arrow);
            this.mDislikeRelativeLayout.findViewById(com.ss.android.article.browser.R.id.t8);
            this.mDislikeRelativeLayout.findViewById(com.ss.android.article.browser.R.id.t9);
            this.mBottomArrow = this.mDislikeRelativeLayout.findViewById(com.ss.android.article.browser.R.id.bottom_arrow);
            this.q = this.mDislikeRelativeLayout.findViewById(com.ss.android.article.browser.R.id.t6);
            this.r = this.mDislikeRelativeLayout.findViewById(com.ss.android.article.browser.R.id.t7);
            this.r.setDivider(null);
            this.s = new a(this, this.t);
            this.r.setAdapter(this.s);
            this.mDislikeRelativeLayout.a(this.q);
            this.mDislikeRelativeLayout.requestLayout();
            this.e();
        }

        this.setContentView(this.mDislikeRelativeLayout);
        this.setCanceledOnTouchOutside(true);
        this.setCancelable(true);
        Window window = this.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        window.setGravity(53);
        attributes.x = 0;
        attributes.y = 0;
        attributes.width = -1;
        attributes.height = -2;
        attributes.alpha = 1f;
        attributes.windowAnimations = 0;
        attributes.dimAmount = 0.3f;
        window.setAttributes(attributes);
        this.mDislikeRelativeLayout.setCallback(new DislikeRelativeLayout.a() {

            public final void a() {
                f();
            }
        });
        if (this.mCellRef.actionList == null) {
            this.mCellRef.actionList = new ArrayList();
        }

        List v0_1 = this.mCellRef.actionList;
        ai.a(v0_1);
        if (v0_1 != null && v0_1.size() > 0) {
            this.t.clear();
            this.t.addAll(((Collection) v0_1));
            this.h = this.t.size();
        }
    }

    private static void a(List arg2) {
        if (arg2 != null && arg2.size() != 0) {
            Iterator v0 = arg2.iterator();
            while (v0.hasNext()) {
                v0.next();
            }
        }
    }

    static void a(View arg6) {
        boolean v3 = true;
        if (arg6 != null) {
            Object v1 = arg6.getTag();
            if ((v1 instanceof FilterWord)) {
                Object v0 = v1;
                boolean v5 = arg6.isSelected();
                boolean v2 = !v5 ? true : false;
                arg6.setSelected(v2);
                if (v5) {
                    v3 = false;
                }

                ((FilterWord) v0).c = v3;
                if (android.support.design.a.isTextEmpty(((FilterWord) v0).a)) {
                    return;
                }

                if (v5) {
                    ai.k.remove(((FilterWord) v0).a);
                    return;
                }

                ai.k.put(((FilterWord) v0).a, v1);
            }
        }
    }

    private boolean a(boolean arg13, Animation.AnimationListener arg14) {
        int v10;
        int v9;
        boolean v0;
        int v3 = this.mDislikeRelativeLayout.getMeasuredWidth();
        int v4 = this.mDislikeRelativeLayout.getMeasuredHeight();
        if (!this.c()) {
            v0 = false;
        } else {
            float v6 = 0f;
            float v8 = 0f;
            if (this.mTopArrow.getVisibility() != View.VISIBLE || !(this.mTopArrow.getLayoutParams() instanceof ViewGroup.MarginLayoutParams)) {
                if (this.mBottomArrow.getVisibility() == View.VISIBLE && ((this.mBottomArrow.getLayoutParams() instanceof ViewGroup.MarginLayoutParams))) {
                    v6 = 1f - (((float) (((ViewGroup.MarginLayoutParams) this.mBottomArrow.getLayoutParams()).rightMargin + this.mBottomArrow.getWidth() / 3))) / (((float) v3));
                    v8 = 1f;
                    v9 = 0;
                    v10 = this.d() - v4;
                } else {
                    v9 = 0;
                    v10 = 0;
                }
            } else {
                v6 = 1f - (((float) (((ViewGroup.MarginLayoutParams) this.mTopArrow.getLayoutParams()).rightMargin + this.mTopArrow.getWidth() / 3))) / (((float) v3));
                v8 = 0f;
                v9 = this.d() - v4;
                v10 = 0;
            }

            if (v6 < 0f) {
                v6 = 0f;
            } else if (v6 > 1f) {
                v6 = 1f;
            }

            AnimationSet v11 = new AnimationSet(false);
            float v1 = arg13 ? 0f : 1f;
            float v2 = arg13 ? 1f : 0f;
            float v3_1 = arg13 ? 0f : 1f;
            float v4_1 = arg13 ? 1f : 0f;
            ScaleAnimation v0_1 = new ScaleAnimation(v1, v2, v3_1, v4_1, 1, v6, 1, v8);
            v0_1.setInterpolator(new OvershootInterpolator(1.5f));
            v0_1.setAnimationListener(arg14);
            v0_1.setDuration(300);
            if (!arg13) {
                AlphaAnimation v1_1 = new AlphaAnimation(1f, 0f);
                v1_1.setInterpolator(new DecelerateInterpolator(3f));
                v1_1.setDuration(300);
                v11.addAnimation(((Animation) v1_1));
            }

            v11.addAnimation(((Animation) v0_1));
            this.mDislikeRelativeLayout.clearAnimation();
            this.mDislikeRelativeLayout.startAnimation(((Animation) v11));
            if (arg13) {
                this.r.setPadding(this.r.getPaddingLeft(), v10, this.r.getPaddingRight(), v9);
            }

            v0 = true;
        }

        return v0;
    }

    public final void a() {
        this.g = null;
    }

    public final void a(int arg8) {
        int v6 = R.id.t7;
        int v5 = 8;
        int v4 = 6;
        Window v0 = this.getWindow();
        WindowManager.LayoutParams v1 = v0.getAttributes();
        v0.setGravity(53);
        v1.x = 0;
        v1.y = arg8;
        v0.setAttributes(v1);
        ViewGroup.LayoutParams v0_1 = this.q.getLayoutParams();
        ((RelativeLayout.LayoutParams) v0_1).height = this.r.getHeight() - this.r.getPaddingBottom() - this.r.getPaddingTop();
        if (this.u) {
            ((RelativeLayout.LayoutParams) v0_1).addRule(v4, v6);
            ((RelativeLayout.LayoutParams) v0_1).addRule(v5, 0);
        } else {
            ((RelativeLayout.LayoutParams) v0_1).addRule(v4, 0);
            ((RelativeLayout.LayoutParams) v0_1).addRule(v5, v6);
        }

        this.q.setLayoutParams(v0_1);
        this.show();
        this.a(true, null);
    }

    public final void a(boolean arg5) {
        int v2 = 8;
        this.u = arg5;
        ImageView v3 = this.mTopArrow;
        int v0 = arg5 ? 0 : v2;
        UIUtils.setVisibility(((View) v3), v0);
        ImageView v0_1 = this.mBottomArrow;
        if (!arg5) {
            v2 = 0;
        }

        UIUtils.setVisibility(((View) v0_1), v2);
        this.mDislikeRelativeLayout.invalidate();
    }

    public final int b() {
        return this.mDislikeRelativeLayout.getMeasuredHeight();
    }

    public final boolean c() {
        boolean v0 = this.mDislikeRelativeLayout.getMeasuredWidth() <= 0 || this.mDislikeRelativeLayout.getMeasuredHeight() <= 0 ? false : true;
        return v0;
    }

    public final void cancel() {
        this.f();
    }

    public final int d() {
        return (UIUtils.getScreenHeight(this.p) - UIUtils.getStatusBarHeight(this.p)) / 2 - com.ss.android.article.base.feature.app.a.b.b - (((int) UIUtils.dp2px(this.p, 10f)));
    }

    public final void e() {
        int v5 = 11;
        ViewGroup.LayoutParams v0 = this.mMainLayout.getLayoutParams();
        int v1 = this.a.getDimensionPixelSize(R.dimen.dw);
        if (UIUtils.getScreenWidth(this.p) > this.a.getDimensionPixelSize(R.dimen.list_item_horizontal_outside_padding) * 2 + v1) {
            ((RelativeLayout.LayoutParams) v0).addRule(v5);
            ((RelativeLayout.LayoutParams) v0).width = v1;
        } else {
            ((RelativeLayout.LayoutParams) v0).addRule(v5);
            ((RelativeLayout.LayoutParams) v0).addRule(9);
            ((RelativeLayout.LayoutParams) v0).width = -1;
        }

        this.mMainLayout.setLayoutParams(v0);
    }

    public final void f() {
        if ((this.isShowing()) && (this.g()) && !this.a(false, new Animation.AnimationListener() {

            public final void onAnimationEnd(Animation arg2) {
                if((isShowing()) && (g())) {
                    dismiss();
                }
            }

            public final void onAnimationRepeat(Animation arg1) {
            }

            public final void onAnimationStart(Animation arg1) {
            }
        }) && (this.isShowing())) {
            this.dismiss();
        }
    }

    public final void onWindowFocusChanged(boolean arg2) {
        if (this.g != null) {
            this.g.a(this);
        }
    }

    final class a extends BaseAdapter {
        private final ai b;
        private List a;

        public a(ai arg1, List arg2) {
            this.b = arg1;

            this.a = arg2;
        }

        public final int getCount() {
            return this.a.size();
        }

        public final Object getItem(int arg2) {
            return this.a.get(arg2);
        }

        public final long getItemId(int arg3) {
            return ((long) arg3);
        }

        public final View getView(int arg8, View arg9, ViewGroup arg10) {
            View v0_1;
            int v0;
            View v2 = this.b.mLayoutInflater.inflate(com.ss.android.article.browser.R.layout.da, arg10, false);
            if (this.a == null || this.a.size() == 0) {
                v0_1 = v2;
            } else {
                if (this.a.size() == 1) {
                    v0 = R.drawable.ec;
                } else if (arg8 == 0) {
                    v0 = R.drawable.ed;
                } else if (arg8 == this.a.size() - 1) {
                    v0 = R.drawable.ea;
                } else {
                    v0 = R.drawable.eb;
                }

                v2.setBackgroundResource(v0);
                v2.setTag(this.a.get(arg8));
                v0_1 = v2.findViewById(com.ss.android.article.browser.R.id.t3);
                View v1 = v2.findViewById(com.ss.android.article.browser.R.id.t4);
                v2.setOnClickListener(this.b.l);
                ((ImageView) v0_1).setImageResource(0);
                ((TextView) v1).setSingleLine(true);
                ((TextView) v1).setText("");
                ((TextView) v1).setEllipsize(TextUtils.TruncateAt.valueOf("END"));
                if (arg8 == this.b.h - 1) {
                    UIUtils.setVisibility(v2.findViewById(com.ss.android.article.browser.R.id.divider_line), 4);
                }

                v0_1 = v2;
            }

            return v0_1;
        }
    }
}

